package br.com.toscano.contpedido.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class HardUtils {

	

	// public static final int TIPO_CPU_BY_WMIC = 2;
	public static final String SEPARACAO = "___";
	public static final String SERIAL_HD_NAO_ENCOTNRADO = "SERIAL_HD_NAO_ENCOTNRADO";
	public static final String MAC_NAO_ENCOTNRADO = "MAC_NAO_ENCOTNRADO";

	public static String getId(String produto) throws Throwable {
		String retorno = null;

		String sistema = System.getProperty("os.name");
	    // System.out.println("Voce esta usando: "+sistema);

		if (sistema.indexOf("indow") != -1) {
			retorno = (getDiskDriverIdByWMIC()+"").trim();
			retorno = retorno + SEPARACAO + (getMAC()+"").trim();
		} else {
			retorno = (getDiskDriverIdByWMICLinux()+"").trim();
			retorno = retorno + SEPARACAO + (getMACLinux()+"").trim();
		}
		retorno = retorno + SEPARACAO + getIdentificacaoNoArquivo().trim();
		return (retorno+ SEPARACAO + produto + "").trim();
	}

	private static String getIdentificacaoNoArquivo() throws Exception {
		String retorno = "";
		File file = new File("licenca.txt");
		if (!file.exists()) {
			FileOutputStream outPut = null;
			try {
				Date dataCriacao = new Date();
				outPut = new FileOutputStream(file);
				outPut.write((dataCriacao.getTime() + "").getBytes());
				retorno = (dataCriacao.getTime() + "");
				System.out.println("Criou o arquivo " + file.toString());
			} catch (Exception e) {
				throw new Exception("Não conseguiu criar o arquivo " + file.toString());
			} finally {
				if (outPut != null) {
					outPut.close();
				}
			}
		} else {
			BufferedReader br = new BufferedReader(new FileReader(file));
			try {
				retorno = br.readLine();
			} finally {
				br.close();
			}
		}
		return retorno;
	}

	private static String getDiskDriverIdByWMICLinux() throws Throwable {
		try {

			Process process = Runtime.getRuntime().exec(new String[] { "wmic", "bios", "get", "serialnumber" });
			process.getOutputStream().close();
			Scanner sc = new Scanner(process.getInputStream());
			String property = sc.next();
			String serial = sc.next();
			return serial;
		} catch (java.io.IOException e) {
			return SERIAL_HD_NAO_ENCOTNRADO;
		}
	}

	private static String getDiskDriverIdByWMIC() throws Throwable {
		try {

			Process process = Runtime.getRuntime().exec(new String[] { "wmic", "bios", "get", "serialnumber" });
			process.getOutputStream().close();
			Scanner sc = new Scanner(process.getInputStream());
			String property = sc.next();
			String serial = sc.next();
			return serial;
		} catch (java.io.IOException e) {
			return SERIAL_HD_NAO_ENCOTNRADO;
		}
	}

	private static String getMAC() throws Exception {
		String retorno = "";
		try {

			String command = "ipconfig /all";
			Process p = Runtime.getRuntime().exec(command);
	 
			BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream()));
			Pattern pattern = Pattern.compile(".*Physical Addres.*: (.*)");
			Pattern pattern2 = Pattern.compile(".*Endere.*o F.*sico.*: (.*)");
			 
				while (true) {
				String line = inn.readLine();

					if (line == null)
					break;

					Matcher mm = pattern.matcher(line);
					if (mm.matches()) {
						retorno = mm.group(1);
						break;
				} else {
					mm = pattern2.matcher(line);
					if (mm.matches()) {
						retorno = mm.group(1);
						break;
					}
					}
				}

		} catch (java.io.IOException e) {
			return MAC_NAO_ENCOTNRADO;
			}
		return retorno;
	}

	private static String getMACLinux() throws Exception {
		String retorno = "";
		try {

			String command = "ifconfig";
			Process p = Runtime.getRuntime().exec(command);

			BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream()));
			Pattern pattern = Pattern.compile(".*ddres.*oHW (.*)");
			Pattern pattern2 = Pattern.compile(".*Endere.*o de HW (.*)");

			while (true) {
				String line = inn.readLine();

				if (line == null)
					break;

				Matcher mm = pattern.matcher(line);
				if (mm.matches()) {
					retorno = mm.group(1);
					break;
				} else {
					mm = pattern2.matcher(line);
					if (mm.matches()) {
						retorno = mm.group(1);
						break;
					}
				}
			}

			} catch (java.io.IOException e) {
			return MAC_NAO_ENCOTNRADO;
		}
		return retorno;
		}


	
}
